package org.dromara.datacheck.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.datacheck.domain.JstExcludeMaterial;
import org.dromara.datacheck.domain.bo.JstExcludeMaterialBo;
import org.dromara.datacheck.domain.vo.JstExcludeMaterialImportVo;
import org.dromara.datacheck.domain.vo.JstExcludeMaterialVo;
import org.dromara.datacheck.listener.JstExcludeMaterialImportListener;
import org.dromara.datacheck.service.IJstExcludeMaterialService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 聚水潭需要排除的物料
 *
 * @author 刘武贵
 * @date 2024-09-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataCheck/excludeMaterial")
public class JstExcludeMaterialController extends BaseController {

    private final IJstExcludeMaterialService jstExcludeMaterialService;

    /**
     * 查询聚水潭需要排除的物料列表
     */
    @SaCheckPermission("dataCheck:excludeMaterial:list")
    @GetMapping("/list")
    public TableDataInfo<JstExcludeMaterialVo> list(JstExcludeMaterialBo bo, PageQuery pageQuery) {
        return jstExcludeMaterialService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出聚水潭需要排除的物料列表
     */
    @SaCheckPermission("dataCheck:excludeMaterial:export")
    @Log(title = "聚水潭需要排除的物料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(JstExcludeMaterialBo bo, HttpServletResponse response) {
        List<JstExcludeMaterialVo> list = jstExcludeMaterialService.queryList(bo);
        ExcelUtil.exportExcel(list, "聚水潭需要排除的物料", JstExcludeMaterialVo.class, response);
    }

    /**
     * 获取聚水潭需要排除的物料详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("dataCheck:excludeMaterial:query")
    @GetMapping("/{id}")
    public R<JstExcludeMaterialVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(jstExcludeMaterialService.queryById(id));
    }

    /**
     * 新增聚水潭需要排除的物料
     */
    @SaCheckPermission("dataCheck:excludeMaterial:add")
    @Log(title = "聚水潭需要排除的物料", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody JstExcludeMaterialBo bo) {
        return toAjax(jstExcludeMaterialService.insertByBo(bo));
    }

    /**
     * 修改聚水潭需要排除的物料
     */
    @SaCheckPermission("dataCheck:excludeMaterial:edit")
    @Log(title = "聚水潭需要排除的物料", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody JstExcludeMaterialBo bo) {
        return toAjax(jstExcludeMaterialService.updateByBo(bo));
    }

    /**
     * 删除聚水潭需要排除的物料
     *
     * @param ids 主键串
     */
    @SaCheckPermission("dataCheck:excludeMaterial:remove")
    @Log(title = "聚水潭需要排除的物料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(jstExcludeMaterialService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "聚水潭需要排除的物料导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        // 每次导入之前清空表里的数据
        List<JstExcludeMaterial> jstExcludeMaterialList = jstExcludeMaterialService.queryAll();
        if(CollUtil.isNotEmpty(jstExcludeMaterialList)){
            jstExcludeMaterialService.deleteAll();
        }
        // 解析Excel文件并处理导入逻辑
        ExcelResult<JstExcludeMaterialImportVo> result = ExcelUtil.importExcel(file.getInputStream(), JstExcludeMaterialImportVo.class, new JstExcludeMaterialImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }
}
